package com.ruoyi.common.utils.http;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpInteractionUtil {

	/*public static String doGet(String url, Map<String, String> param) {
		// 创建Httpclient对象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String resultString = "";
		CloseableHttpResponse response = null;
		try {
			// 创建uri
			URIBuilder builder = new URIBuilder(url);
			if (param != null) {
				for (String key : param.keySet()) {
					builder.addParameter(key, param.get(key));
				}
			}
			URI uri = builder.build();
			// 创建http GET请求
			HttpGet httpGet = new HttpGet(uri);
			// 执行请求
			response = httpclient.execute(httpGet);
			if (response.getEntity() != null) {
				resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (response != null) {
					response.close();
				}
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultString;
	}*/

	/*public static String executeHttpPost(String url,String json){
		try {
			//建立连接
			CloseableHttpClient client = HttpClients.createDefault();
			HttpPost post = new HttpPost(url);
			post.addHeader("Content-Type","application/json");
			//内容
			StringEntity entity = new StringEntity(json, ContentType.create("application/json", "utf-8"));
			post.setEntity(entity);
			//执行
			CloseableHttpResponse resp = client.execute(post);
			//返回
			int sc = resp.getStatusLine().getStatusCode();
			if(sc>=200 && sc<300) {
				return EntityUtils.toString(resp.getEntity());
			}
		} catch (UnsupportedCharsetException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}*/

	public static String executeHttpGet(String url){
		try {
			//建立连接
			CloseableHttpClient client = HttpClients.createDefault();
			HttpGet get = new HttpGet(url);
			CloseableHttpResponse resp = client.execute(get);
			int statusCode = resp.getStatusLine().getStatusCode();
			if(statusCode>=200 && statusCode<300) {
				HttpEntity entity = resp.getEntity();
				return EntityUtils.toString(entity,"utf-8");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getDate(String rcwUrl, String enc) {
		StringBuffer strBuf = new StringBuffer();
		try {
			URL url = new URL(rcwUrl);
			URLConnection conn = url.openConnection();
			conn.setConnectTimeout(3000);
			conn.setReadTimeout(3000);
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), enc));
			for (String line = null; (line = reader.readLine()) != null;)
				strBuf.append((new StringBuilder(String.valueOf(line))).append(" ").toString());

			reader.close();
		} catch (MalformedURLException malformedurlexception) {
		} catch (IOException ioexception) {
		}
		return strBuf.toString();
	}
	/*public static String getDateByPost(String rcwUrl, String enc) {
		StringBuffer strBuf = new StringBuffer();
		try {
			URL url = new URL(rcwUrl);
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			connection.setRequestMethod("post");  //请求方式
			connection.setReadTimeout(2000);   //读取数据超时
			connection.setDoOutput(true);
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), enc));
			for (String line = null; (line = reader.readLine()) != null;)
				strBuf.append((new StringBuilder(String.valueOf(line))).append(" ").toString());
			reader.close();
		} catch (MalformedURLException malformedurlexception) {
		} catch (IOException ioexception) {
		}
		return strBuf.toString();
	}*/
}
